package protocol

import (
	"fmt"
	"io"
)

type Writer struct {
	writer io.Writer
}

func NewWriter(writer io.Writer) *Writer {
	return &Writer{
		writer: writer,
	}
}

func (w *Writer) writerString(msg string) error {
	_, err := w.writer.Write([]byte(msg))
	return err
}

func (w *Writer) Writer(command interface{}) error {
	var err error

	switch v := command.(type) {
	case SendCmd:
		err = w.writerString(fmt.Sprintf("SEND %v\n", v.Message))
	case MessCmd:
		err = w.writerString(fmt.Sprintf("MESS %v %v\n",
			v.Name,
			v.Message))
	case NameCmd:
		err = w.writerString(fmt.Sprintf("NAME %v\n", v.Name))
	default:
		err = UnknowCommand
	}
	return err
}
